package com.fling;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.physics.box2d.World;
import com.fling.device.Device;
import com.fling.services.AccomplishmentsOutbox;
import com.fling.services.Achievements;
import com.fling.services.GoogleServices;
import com.fling.sound.ConsoleAudio;

public class Locator implements Constants {
	private static ConsoleAudio audioService;
	private static ArrayList<ParticleEffect> effects;
	private static InputMultiplexer multi;
	private static FlingHero game;
	private static World world;
	private static float xSpriteScale, ySpriteScale;
	private static Rectangle viewport;
	public static float blockSpacing;
	public static float xGameSpeed, yGameSpeed;
	public static float xHardGameSpeed, yHardGameSpeed, xEasyGameSpeed, yEasyGameSpeed;
	public static int topEasyDistance, topHardDistance;
	public static boolean tutorialCompleted;
	public static float overallGameSpeed;
	public static int difficulty;
	public static int coins;
	public static boolean mute;
	public static GoogleServices services;
	public static Device device;
	public static AccomplishmentsOutbox outbox;
	public static Achievements achievements;

	public static int HARD = 1;
	public static int EASY = 0;

	public static void init(FlingHero g, Device d, GoogleServices s) {
		services=s;
		device=d;
		
		achievements=new Achievements();
		outbox=new AccomplishmentsOutbox(achievements);
		
		
		effects = new ArrayList<ParticleEffect>();
		multi = new InputMultiplexer();
		game = g;
		xSpriteScale = 1;
		ySpriteScale = 1;
		overallGameSpeed = 1;
		difficulty = EASY;
		viewport = new Rectangle(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		
		Locator.readSave();
		
		audioService = new ConsoleAudio();
		audioService.setMute(mute);
	}

	public static void setSpriteScale(float x_scale, float y_scale, Rectangle view) {
		Locator.setEasyGameSpeed(x_scale * 2, y_scale * 2);
		Locator.setHardGameSpeed(x_scale * 4, y_scale * 4);
		Locator.useDifficultySpeed();
		xSpriteScale = x_scale;
		ySpriteScale = y_scale;
		viewport.set(view);
		blockSpacing = (VIRTUAL_WIDTH * xSpriteScale - WALL_WIDTH * xSpriteScale * 2) / NUM_OF_BLOCKS;
	}

	public static void setDifficutly(int d) {
		difficulty = d;
		Locator.useDifficultySpeed();
	}

	public static void setWorld(World w) {
		world = w;
	}

	public static void win() {
		writeSave();
		audioService.stopMusic();
	}

	public static void readSave() {
		Preferences prefs = Gdx.app.getPreferences("My Preferences");
		topHardDistance = prefs.getInteger("top hard distance", 0);
		topEasyDistance = prefs.getInteger("top easy distance", 0);
		tutorialCompleted = prefs.getBoolean("tutorial", false);
		coins = prefs.getInteger("coins", 0);
		mute = prefs.getBoolean("mute", false);
		outbox.loadLocal(prefs, topHardDistance);
	}

	public static void writeSave() {
		Preferences prefs = Gdx.app.getPreferences("My Preferences");
		prefs.putInteger("top hard distance", topHardDistance);
		prefs.putInteger("top easy distance", topEasyDistance);
		prefs.putBoolean("tutorial", tutorialCompleted);
		prefs.putInteger("coins", coins);
		prefs.putBoolean("mute", mute);
		outbox.saveLocal(prefs, topHardDistance);
		prefs.flush();
	}

	public static void setNewDistance(int d) {
		if (Locator.difficulty == Locator.HARD) {
			if (d > topHardDistance) {
				topHardDistance = d;
			}
		} else {
			if (d > topEasyDistance) {
				topEasyDistance = d;
			}
		}
	}
	
	public static void setMute(boolean m){
		mute=m;
		audioService.setMute(m);
	}

	public static World getWorld() {
		return world;
	}
	
	public static Achievements getAchievements(){
		return achievements;
	}

	public static InputMultiplexer getInputMultiplexer() {
		return multi;
	}

	public static ConsoleAudio getAudio() {
		return audioService;
	}
	
	public static AccomplishmentsOutbox getAccomplishments(){
		return outbox;
	}

	public static ArrayList<ParticleEffect> getEffects() {
		return effects;
	}

	public static FlingHero getGame() {
		return game;
	}

	public static boolean isMute() {
		return mute;
	}

	public static float getxSpriteScale() {
		return xSpriteScale;
	}

	public static float getySpriteScale() {
		return ySpriteScale;
	}

	public static Rectangle getViewport() {
		return viewport;
	}

	public static float getBlockSpacing() {
		return blockSpacing;
	}

	public static float getXGameSpeed() {
		return xGameSpeed;
	}

	public static float getYGameSpeed() {
		return yGameSpeed;
	}

	public static void setGameSpeed(float x_gameSpeed, float y_gameSpeed) {
		Locator.xGameSpeed = 2 * x_gameSpeed;
		Locator.yGameSpeed = 2 * y_gameSpeed;
	}

	public static void setEasyGameSpeed(float x_gameSpeed, float y_gameSpeed) {
		Locator.xEasyGameSpeed = 2 * x_gameSpeed;
		Locator.yEasyGameSpeed = 2 * y_gameSpeed;
	}

	public static void setHardGameSpeed(float x_gameSpeed, float y_gameSpeed) {
		Locator.xHardGameSpeed = 2 * x_gameSpeed;
		Locator.yHardGameSpeed = 2 * y_gameSpeed;
	}

	public static void useEasySpeed() {
		Locator.xGameSpeed = Locator.xEasyGameSpeed;
		Locator.yGameSpeed = Locator.yEasyGameSpeed;
	}

	public static void useHardSpeed() {
		Locator.xGameSpeed = Locator.xHardGameSpeed;
		Locator.yGameSpeed = Locator.yHardGameSpeed;
	}

	public static void setTutorialCompleted(boolean tutorialCompleted) {
		Locator.tutorialCompleted = tutorialCompleted;
	}

	public static int getTopDistance() {
		if(Locator.difficulty==Locator.HARD){
			return topHardDistance;
		} else {
			return topEasyDistance;
		}
	}

	public static Device getDevice() {
		return device;
	}

	public static void setDevice(Device device) {
		Locator.device = device;
	}

	public static void setTopDistance(int topDistance) {
		if(Locator.difficulty==Locator.HARD){
			topHardDistance=topDistance;
		} else {
			topEasyDistance=topDistance;
		}
	}

	public static boolean isTutorialCompleted() {
		return tutorialCompleted;
	}

	public static void setOverallGameSpeed(float i) {
		overallGameSpeed = i;

	}

	public static float getOverallGameSpeed() {
		return overallGameSpeed;
	}

	public static void useDifficultySpeed() {
		if (difficulty == HARD) {
			Locator.useHardSpeed();
		} else {
			Locator.useEasySpeed();
		}

	}

	public static int getCoins() {
		return coins;
	}

	public static void increaseCoins() {
		coins++;
	}

	public static GoogleServices getServices() {
		return services;
	}

	public static void setServices(GoogleServices services) {
		Locator.services = services;
	}

}
